package com.chainyoung.component_tmperson.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.common_widget.loadsir.callback.Callback;
import com.chainyoung.common.common_widget.loadsir.core.LoadService;
import com.chainyoung.common.common_widget.loadsir.core.LoadSir;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.custom_widget.EmptyCallback;
import com.chainyoung.common.custom_widget.LoadingCallback;
import com.chainyoung.common.custom_widget.RetryCallback;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.Reward;
import com.chainyoung.component_tmperson.entity.WalletProfiRecord;
import com.chainyoung.component_tmperson.ui.adapter.RewardAdapter;
import com.chainyoung.component_tmperson.ui.adapter.WalletFianancingProfitAdapter;
import com.chainyoung.component_tmperson.ui.mvp.contract.RewardContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.RewardPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RewardActivity extends BaseActivity<RewardPresenter> implements RewardContract.View {

    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.refresh_layout)
    SmartRefreshLayout refreshLayout;


    private RewardAdapter mAdapter;


    private List<Reward> mData = new ArrayList<>();
    private boolean isCanloadMore;
    protected int pageNo = 1;

    private LoadService loadService;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }


    @Override
    protected void initUI() {
        setCurrentTitle("我的奖励");
        initRecyclerView();
        initLoadSir();
    }

    private void initLoadSir() {
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new LoadingCallback())
                .addCallback(new EmptyCallback())
                .addCallback(new RetryCallback())
                .setDefaultCallback(LoadingCallback.class)
                .build();
        loadService = loadSir.register(refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                pageNo = 1;
                getData();
            }
        });
    }

    private void initRecyclerView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new RewardAdapter(mData);
        rvList.setAdapter(mAdapter);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_divider_gray));
        rvList.addItemDecoration(itemDecoration);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                getData();
            }
        });

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isCanloadMore) {
                    pageNo++;
                    getData();
                } else {
                    refreshLayout.finishLoadMore();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                getData();

            }
        });
    }

    @Override
    protected void initData() {
        pageNo = 1;
        getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_my_reward;
    }


    private void getData() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put(LibConstant.TOKEN, SPUtil.getString(LibConstant.TOKEN));
        map.put("page", pageNo);
        map.put("limit", LibConstant.PAGE_SIZ);
        mPresenter.getRewardList(map);
    }


    @Override
    public void onGetRewardList(CommonList<Reward> data) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (pageNo == 1) {
            mData.clear();
        }
        if (data == null) {
            isCanloadMore = false;
            refreshLayout.setEnableLoadMore(false);
            if (pageNo == 1) {
                loadService.showCallback(EmptyCallback.class);
            }
            return;
        }
        mData.addAll(data.getList());
        if (data.getList().size() < LibConstant.PAGE_SIZ) {
            isCanloadMore = false;
            refreshLayout.setEnableLoadMore(false);
        } else {
            isCanloadMore = true;
            refreshLayout.setEnableLoadMore(true);
        }
        mAdapter.notifyDataSetChanged();
        if (mData.isEmpty()) {
            loadService.showCallback(EmptyCallback.class);
        } else {
            loadService.showSuccess();

        }

    }

    @Override
    public void onGetRewardListFailed() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        loadService.showCallback(RetryCallback.class);
    }


}
